/**
 * AdminIntfHandler.java
 */

package wyse.server.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.HandlerWrapper;

/*
 * Handler for the admin interface (accepts only localhost connections)
 */
public class AdminIntfHandler extends HandlerWrapper {
    @Override
    public void handle(String target, Request baseRequest,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException
    {
        if(isAccepted(baseRequest)) {
            super.handle(target, baseRequest, request, response);
        } else {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            baseRequest.setHandled(true);
        }
    }

    private static boolean isAccepted(Request req) {
        Connector c = req.getConnection().getConnector();
        String host = c.getHost();
        if(host != null && host.equals("localhost"))
            return true;

        return false;
    }
}
